package com.chen.shopping_admin_service.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_admin_service.mapper.RoleMapper;
import com.chen.shopping_common.pojo.Role;
import com.chen.shopping_common.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void deleteRole(Long rid) {
        roleMapper.deleteById(rid);
        roleMapper.deleteRolePermission(rid);
        roleMapper.deleteRoleAdmin(rid);
    }

    @Override
    public Role findRoleById(Long rid) {
        return roleMapper.findRolePermission(rid);
    }

    @Override
    public List<Role> findAllRole() {
        return roleMapper.selectList(null);
    }

    @Override
    public Page<Role> findRoleByPage(int page,int size) {
        return roleMapper.selectPage(new Page<>(page,size),null);
    }

    @Override
    public void updateRolePermission(Long rid, Long[] pids) {
        roleMapper.deleteRolePermission(rid);
        for (Long pid : pids) {
            roleMapper.addPermissionToRole(rid,pid);
        }
    }
}
